package cn.yilongquan.service.impl;

import cn.yilongquan.constant.CacheConstant;
import cn.yilongquan.mapper.ArticleTypeMapper;
import cn.yilongquan.service.ArticleTypeService;
import cn.yilongquan.user.domain.ArticleType;
import cn.yilongquan.user.query.ArticleTypeQuery;
import cn.yilongquan.util.R;
import cn.yilongquan.util.RandomStringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Override
    public ArticleType findById(Long id) {
        return articleTypeMapper.findById(id);
    }

    @Override
    public List<ArticleType> findAll() {
        return articleTypeMapper.findAll();
    }

    @Override
    public void add(ArticleType articleType) {
        redisTemplate.delete(CacheConstant.ARTICLE_TYPE);
        articleTypeMapper.add(articleType);
    }

    @Override
    public void update(ArticleType articleType) {
        redisTemplate.delete(CacheConstant.ARTICLE_TYPE);

        articleTypeMapper.update(articleType);
    }

    @Override
    public void deleteById(Long id) {
        redisTemplate.delete(CacheConstant.ARTICLE_TYPE);

        articleTypeMapper.deleteById(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        redisTemplate.delete(CacheConstant.ARTICLE_TYPE);

        articleTypeMapper.batchDelete(ids);
    }

    @Override
    public R pageList(ArticleTypeQuery articleTypeQuery) {
        //自动分页
        PageHelper.startPage(articleTypeQuery.getPage(), articleTypeQuery.getSize());
        List<ArticleType> articleTypes = articleTypeMapper.pageList(articleTypeQuery);
        return R.ok(new PageInfo<ArticleType>(articleTypes));
    }

    @Override
    public R getTree() {
        Object o = redisTemplate.opsForValue().get(CacheConstant.ARTICLE_TYPE);
        if (o != null) {
            return R.ok(o);
        }
        List<ArticleType> articleTypes = articleTypeMapper.findAll();
        Map<Long,ArticleType> map = new HashMap<>();
        for (ArticleType articleType : articleTypes) {
            //主键做key，本身当value
            map.put(articleType.getId(), articleType);
        }
        ArrayList<ArticleType> result = new ArrayList<>();
        for (ArticleType articleType : articleTypes) {
            //判断当前记录是不是父记录
            if(articleType.getParentId() == null){
                //添加父类
                result.add(articleType);
            }else{
              Long parentId =  articleType.getParentId();
                ArticleType articleType1 = map.get(parentId);
                articleType1.getChildren().add(articleType);


            }
        }
        redisTemplate.opsForValue().set(CacheConstant.ARTICLE_TYPE, result);
        return R.ok(result);
    }
}